package com.nep.controller;

import com.nep.service.SupervisorService;
import com.nep.service.impl.SupervisorServiceImpl;
import com.nep.util.JavafxUtil;
import com.nep.NepsMain;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.PasswordField;
import javafx.stage.Stage;
import javafx.scene.control.TextField;  // 确保导入的是 JavaFX 的 TextField
import javafx.scene.*;
import java.lang.*;
import java.util.*;
import javafx.scene.control.Alert;


public class NepsLoginViewController {

    @FXML
    private TextField txt_id;	//绑定登录账号文本框
    @FXML
    private PasswordField txt_password;	//绑定登录密码框

    //主舞台
    public static Stage primaryStage;
    //多态
    public SupervisorService supervisorService = new SupervisorServiceImpl();

    public TextField getTxt_id() {
        return txt_id;
    }
    public void setTxt_id(TextField txt_id) {
        this.txt_id = txt_id;
    }
    public PasswordField getTxt_password() {
        return txt_password;
    }
    public void setTxt_password(PasswordField txt_password) {
        this.txt_password = txt_password;
    }

    /**
     * 绑定登录按钮事件
     */
    public void login(){
        if (txt_id == null || txt_password == null) {
            JavafxUtil.showAlert(primaryStage, "错误", "系统错误", "登录组件未正确初始化", "warn");
            return;
        }
        
        String loginId = txt_id.getText();
        String password = txt_password.getText();
        
        if (loginId == null || loginId.trim().isEmpty()) {
            JavafxUtil.showAlert(primaryStage, "错误", "输入错误", "请输入登录账号", "warn");
            return;
        }
        
        if (password == null || password.trim().isEmpty()) {
            JavafxUtil.showAlert(primaryStage, "错误", "输入错误", "请输入登录密码", "warn");
            return;
        }

        boolean flag = supervisorService.login(loginId, password);
        if(flag){
            NepsSelectAqiViewController.primaryStage = primaryStage;
            JavafxUtil.showStage(NepsLoginViewController.class,"/com/nep/view/NepsSelectAqiView.fxml", primaryStage, "东软环保公众监督平台-公众监督员端-AQI数据反馈");
        }else{
            // 使用自定义ErrorAlertView界面
            try {
                FXMLLoader loader = new FXMLLoader(getClass().getResource("/com/nep/view/ErrorAlertView.fxml"));
                Parent root = loader.load();
                ErrorAlertViewController controller = loader.getController();
                controller.setErrorTitle("登录失败");
                controller.setErrorMessage("用户名密码错误");
                Stage errorStage = new Stage();
                controller.setStage(errorStage); // 传递Stage给Controller
                errorStage.setScene(new Scene(root));
                errorStage.showAndWait();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 绑定注册按钮事件
     */
    public void register(){
        //跳转到公众监督员注册界面
        NepsRegisterViewController.primaryStage = primaryStage;
        JavafxUtil.showStage(NepsLoginViewController.class,"/com/nep/view/NepsRegisterView.fxml", primaryStage, "东软环保公众监督平台-公众监督员端-公众监督员注册");
    }

    /**
     * 绑定找回密码按钮事件
     */
    public void recoverPassword() {
        JavafxUtil.showSubStage(NepsLoginViewController.class, "/com/nep/view/NepsRecoverView.fxml", primaryStage, "找回密码");
    }
}